<?php
$formatter = Yii::app()->format;
?>
<div>
<div>项目 <span class=""><?php echo $project->name; ?></span> 中 
<span class=""><?php echo $materialModel->modelName; ?> 的记录</span>
</div>
<table class="material-project-statistic-table">
	<thead>
	<tr>
		<td rowspan="2">日期</td>
		<td colspan="2">申购记录</td>
		<td colspan="2">入库记录</td>
		<td colspan="5">入账记录</td>
	</tr>
	<tr>
		<td>数量</td>
		<td>申请人</td>
		<td>数量</td>
		<td>经办人</td>
		<td>数量</td>
		<td>单价</td>
		<td>单价（标准）</td>
		<td>总价</td>
		<td>经办人</td>
	</tr>
	</thead>
	<tbody>
<?php
$amount = array(
	'Requisition' => 0.0,
	'WarehouseInbound' => 0.0,
	'Receipt' => 0.0,
	'TotalPrice' => 0.0,
);
$rowCount = 0;
foreach ($result as $date => $r) {
	$maxRecords = max(isset($r['Requisition']) ? count($r['Requisition']) : 0,
		isset($r['WarehouseInbound']) ? count($r['WarehouseInbound']) : 0,
		isset($r['Receipt']) ? count($r['Receipt']) : 0);
	for ($i = 0; $i < $maxRecords; $i++) {
?>
	<tr class="<?php echo $rowCount % 2 ? "row-even" : "row-odd"; ?><?php echo $i == 0 ? ' row-item'
																				: '';
																	?>">
	<?php if ($i == 0) { ?>
		<td rowspan="<?php echo $maxRecords; ?>"><?php echo $date; ?></td>
	<?php } ?>
		<?php if (isset($r['Requisition']) && isset($r['Requisition'][$i])) {
					$amount['Requisition'] += $r['Requisition'][$i]->amountStandardUnit;
		?>
		<td><?php echo CHtml::link(
							$formatter->formatAmount($r['Requisition'][$i]->amountStandardUnit)
								. ' ' . $materialModel->unit,
							array(
								'materialRequisition/view',
								'id' => $r['Requisition'][$i]->materialRequisitionId
							), array(
								'target' => '_blank'
							));
			?></td>
		<td><?php echo $r['Requisition'][$i]->applicant->getFullName(); ?></td>
		<?php } else { ?>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<?php } ?>
		<?php if (isset($r['WarehouseInbound']) && isset($r['WarehouseInbound'][$i])) {
					$amount['WarehouseInbound'] += $r['WarehouseInbound'][$i]->amountStandardUnit;
		?>
		<td><?php echo CHtml::link(
							$formatter
								->formatAmount($r['WarehouseInbound'][$i]->amountStandardUnit)
								. ' ' . $materialModel->unit,
							array(
								'materialWarehouseInbound/view',
								'id' => $r['WarehouseInbound'][$i]->materialWarehouseInboundId
							), array(
								'target' => '_blank'
							));
			?></td>
		<td><?php echo $r['WarehouseInbound'][$i]->materialWarehouseInbound->handler; ?></td>
		<?php } else { ?>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<?php } ?>
		<?php if (isset($r['Receipt']) && isset($r['Receipt'][$i])) {
					$amount['Receipt'] += $r['Receipt'][$i]->amountStandardUnit;
					$amount['TotalPrice'] += $r['Receipt'][$i]->totalPrice;
		?>
		<td><?php echo CHtml::link(
							$formatter->formatAmount($r['Receipt'][$i]->amountStandardUnit) . ' '
								. $materialModel->unit,
							array(
								'materialReceipt/view',
								'id' => $r['Receipt'][$i]->materialReceiptId
							), array(
								'target' => '_blank'
							));
			?></td>
		<td><?php echo $formatter->formatCurrency($r['Receipt'][$i]->unitPrice); ?> /
		<?php echo $r['Receipt'][$i]->unit; ?></td>
		<td><?php echo $formatter->formatCurrency($r['Receipt'][$i]->standardUnitPrice); ?> /
		<?php echo $r['Receipt'][$i]->materialModel->unit; ?></td>
		<td><?php echo $formatter->formatCurrency($r['Receipt'][$i]->totalPrice); ?></td>
		<td><?php echo $r['Receipt'][$i]->materialReceipt->handler; ?></td>
		<?php } else { ?>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<?php } ?>
	</tr>
<?php }
	$rowCount++;
}
?>
	</tbody>
	<tfoot>
	<tr>
		<td>合计</td>
		<td><?php echo $formatter->formatAmount($amount['Requisition']); ?>
		 <?php echo $materialModel->unit; ?></td>
		<td>&nbsp;</td>
		<td><?php echo $formatter->formatAmount($amount['WarehouseInbound']); ?>
		 <?php echo $materialModel->unit; ?></td>
		<td>&nbsp;</td>
		<td><?php echo $formatter->formatAmount($amount['Receipt']); ?>
		 <?php echo $materialModel->unit; ?></td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td><?php echo $formatter->formatCurrency($amount['TotalPrice']); ?></td>
		<td>&nbsp;</td>
	</tr>
	</tfoot>
</table>
</div>
